package com.pxxy.springmvc.service.impl;

import com.pxxy.springmvc.entity.Account;
import com.pxxy.springmvc.mapper.AccountMapper;
import com.pxxy.springmvc.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/******************
 * @Author yejf
 * @Description TODO
 */
@Service  //等价于 @Component
public class AccountServiceImpl implements AccountService {

    @Autowired   //自动注入
    private AccountMapper accountDao;

    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.READ_COMMITTED,
            readOnly = false,
            timeout = 3000,
            rollbackFor = Exception.class,
            noRollbackFor = RuntimeException.class)
    @Override
    public int deposit(Account account, double money) {
        //根据ID来查询出最新的帐户
        Account acct = accountDao.findById(account.getId());
        //判断
        if(acct == null) {
            System.out.println("帐户不存在："+account.getId());
            return -1;
        }
        //存钱操作
        if(money >= 0) {
            acct.setBalance(acct.getBalance() + money);
            //更新回去
            return accountDao.update(acct);
        } else {
            System.out.println("存入的金额不能小于或等于0");
            return -1;
        }
    }

    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.READ_COMMITTED,
            readOnly = false,
            timeout = 3000,
            rollbackFor = Exception.class,
            noRollbackFor = RuntimeException.class)
    @Override
    public int withdraw(Account account, double money) {
        //把帐户查询出来
        Account acct = accountDao.findById(account.getId());
        //判断
        if(acct == null) {
            System.out.println("帐户不存在："+account.getId());
            return -1;
        }
        //进一步判断余额是否足够
        if(acct.getBalance() < money) {
            System.out.println("帐户：["+acct.getId()+"] 余额不够");
            return -1;
        } else {
            //做取钱操作
            acct.setBalance(acct.getBalance() - money);
            //更新回去
            return accountDao.update(acct);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.READ_COMMITTED,
            readOnly = false,
            timeout = 3000,
            rollbackFor = RuntimeException.class)
    @Override
    public int transfer(Account from, Account target, double money) {
        //
        final Account fromAccount = accountDao.findById(from.getId());
        //
        if(fromAccount == null) {
            System.out.println("原帐户["+from.getId()+"]不存在");
            return -1;
        }
        //
        if(fromAccount.getBalance() < money) {
            System.out.println("原帐户:["+from.getId()+"] 余额不足.");
            return -1;
        }
        //目标帐户是否存在
        final Account targetAccount = accountDao.findById(target.getId());
        //
        if(targetAccount == null) {
            System.out.println("目标帐户:["+target.getId()+"] 不存在.");
            return -1;
        } else {
            //1.先更新原帐户
            fromAccount.setBalance(fromAccount.getBalance() - money);
            accountDao.update(fromAccount);
            //
            System.out.println("-- 原帐户更新成功");
            //
            //2.再更新目标帐户
            targetAccount.setBalance(targetAccount.getBalance() + money);
            accountDao.update(targetAccount);

            System.out.println("-- 目标帐户更新成功");
            //
            return 1;
        }
    }
}
